package com.zjm.zdk.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 网络服务
 * 1:1 单线程
 * socket + i/o
 */
public class BIOServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8088);
        System.out.println("服务器启动");
        while (!serverSocket.isClosed()) {
            Socket socket = serverSocket.accept();
            System.out.println("收到请求，来自：" + socket.toString());
            InputStream inputStream = socket.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            String msg;
            while ((msg = reader.readLine()) != null) {
                if (msg.length() == 0) {
                    break;
                }
                System.out.println(msg);
            }
            System.out.println("收到数据，来自：" + socket.toString() + '\n');
            socket.close();
        }
        serverSocket.close();
    }
}


/*


ServerSocket serverSocket = new ServerSocket(8081);
        System.out.println("服务器启动成功");
        while (!serverSocket.isClosed()) {
            Socket socket = serverSocket.accept();
            System.out.println("收到新连接：" + socket.toString());
            InputStream inputStream = socket.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            String msg;
            while ((msg = reader.readLine()) != null) {
                if (msg.length() == 0) {
                    break;
                }
                System.out.println(msg);
            }
            System.out.println("收到数据，来自：" + socket.toString());
            socket.close();
        }
        serverSocket.close();


 */